package com.anjiplus.template.gaea.business.modules.report.controller;

import com.anji.plus.gaea.annotation.Permission;
import com.anji.plus.gaea.annotation.log.GaeaAuditLog;
import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.curd.controller.GaeaBaseController;
import com.anji.plus.gaea.curd.service.GaeaBaseService;
import com.anjiplus.template.gaea.business.modules.dashboard.controller.dto.ReportDashboardObjectDto;
import com.anjiplus.template.gaea.business.modules.dashboard.service.ReportDashboardService;
import com.anjiplus.template.gaea.business.modules.dashboardwidget.controller.dto.ReportDashboardWidgetDto;
import com.anjiplus.template.gaea.business.modules.report.controller.dto.ReportDto;
import com.anjiplus.template.gaea.business.modules.report.controller.param.ReportParam;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import com.anjiplus.template.gaea.business.modules.report.service.ReportService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author chenkening
 * @date 2021/3/26 10:19
 */
@RestController
@Api(tags = "报表数据管理")
@Permission(code = "reportManage", name = "报表管理")
@RequestMapping("/report")
public class ReportController extends GaeaBaseController<ReportParam, Report, ReportDto> {

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportDashboardService reportDashboardService;

    @Override
    public GaeaBaseService<ReportParam, Report> getService() {
        return reportService;
    }

    @Override
    public Report getEntity() {
        return new Report();
    }

    @Override
    public ReportDto getDTO() {
        return new ReportDto();
    }

    @DeleteMapping("/delReport")
    @Permission(code = "delete", name = "删除")
    @GaeaAuditLog(pageTitle = "删除")
    public ResponseBean delReport(@RequestBody ReportDto reportDto) {
        reportService.delReport(reportDto);
        return ResponseBean.builder().build();
    }

    @PostMapping("/copy")
    @Permission(code = "insert", name = "新增")
    @GaeaAuditLog(pageTitle = "复制")
    public ResponseBean copy(@Validated @RequestBody ReportDto dto) {
        Report report = getService().selectOne(dto.getId());

        Report newReport = new Report();
        newReport.setReportName(dto.getReportName());
        newReport.setReportCode(dto.getReportCode());
        newReport.setReportDesc(dto.getReportDesc());
        newReport.setReportGroup(dto.getReportGroup());
        newReport.setDeleteFlag(dto.getDeleteFlag());
        newReport.setEnableFlag(dto.getEnableFlag());
        newReport.setReportImage(dto.getReportImage());
        newReport.setReportType(dto.getReportType());
        reportService.insert(newReport);//插入大屏

        //查询源大屏数据
        ReportDashboardObjectDto detail = reportDashboardService.getDetail(report.getReportCode());
        List<ReportDashboardWidgetDto> widgets = detail.getDashboard().getWidgets();
        detail.setWidgets(widgets);
        detail.getDashboard().setWidgets(null);

        //插入新大屏数据
        detail.setReportCode(newReport.getReportCode());
        reportDashboardService.insertDashboard(detail);

        return ResponseBean.builder().data(newReport).build();
    }
}
